package com.lengmou.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.lengmou.entity.Goods;
import com.lengmou.entity.GoodsType;
import com.lengmou.service.GoodsService;
import com.lengmou.service.GoodsTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/goodstype")
public class GooodsTypeController {
    @Resource
    private GoodsTypeService goodsTypeService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping(value = "/getAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAll(){
        JSONObject json = new JSONObject();
        List<GoodsType> all = goodsTypeService.getAll();
        json.put("types",all);
        return json.toJSONString();
    }

    @RequestMapping(value = "/setTypeName",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setTypeName(GoodsType goodsType){
        JSONObject json = new JSONObject();
        if(goodsType!=null){
            int i = goodsTypeService.setTypeName(goodsType);
            json.put("state",i);
        }else{
//            参数是空的
            json.put("state",-2);
        }
        return json.toString();
    }

    @RequestMapping(value = "/setTypeFromid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setTypeFromid(GoodsType goodsType){
        JSONObject json = new JSONObject();
        if(goodsType!=null){

            int i = goodsTypeService.setTypeFromid(goodsType);
            json.put("state",i);

        }else{
            json.put("state",-2);
        }
        return json.toString();
    }

    @RequestMapping(value = "/addType",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addTypes(GoodsType goodsType){
        JSONObject json = new JSONObject();
        if(goodsType!=null){

            int i = goodsTypeService.addType(goodsType);
            json.put("state",i);

        }else{
            json.put("state",-2);
        }
        return json.toString();
    }

    @RequestMapping(value = "/updata",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updataTypes(GoodsType goodsType){
        JSONObject json = new JSONObject();
        int i = goodsTypeService.updata(goodsType);
        json.put("state",i);
        return json.toString();
    }
    @RequestMapping(value = "/getAllSon",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAllSo(int id){
        JSONObject json = new JSONObject();
       List<GoodsType> list = new ArrayList<>();
        List<GoodsType> allba = getAllba(id, list);
        json.put("father",allba);
        return json.toString();
    }
    @RequestMapping(value = "/getAllFather",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAllFather(int id){
        JSONObject json = new JSONObject();
        List<GoodsType> list = new ArrayList<>();
        List<GoodsType> allba = getAllba(id, list);
        json.put("father",allba);
        return json.toString();
    }

    @RequestMapping(value = "/getAllFB",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAllFatherBother(int id){
        JSONObject json = new JSONObject();
        List<GoodsType> list = new ArrayList<>();
        List<GoodsType> fb = getFB(id,list);
        System.out.println(fb);
        json.put("types",fb);
        return json.toString();
    }
    @RequestMapping(value = "/delete",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delete(int id){
        JSONObject json = new JSONObject();
        List<GoodsType> list = new ArrayList<>();
        List<GoodsType> allSon = getAllSon(id, list);
        int i =0;
        if(allSon.size()!=0){
            Collections.reverse(allSon);
            for (GoodsType g: allSon) {
                List<Goods> goodsByType = goodsService.getGoodsByType(g.getId());
                for (Goods goods: goodsByType) {
                    goods.setType(1000);
                    goodsService.setGoodsType(goods);
                }
                i+=goodsTypeService.deleteType(g.getId());
            }
        }
        List<Goods> goodsByType1 = goodsService.getGoodsByType(id);
        for (Goods g:goodsByType1) {
            g.setType(1000);
            goodsService.setGoodsType(g);
        }
        int i1 = goodsTypeService.deleteType(id);
        json.put("state",i1);
        json.put("son",i);
        return json.toString();
    }




//    获取所有子级
    public List<GoodsType> getAllSon(int id,List<GoodsType> list){
        List<GoodsType> goodsTypes = goodsTypeService.getTypeSons(id);
        for (GoodsType g:goodsTypes) {
            list.add(g);
            getAllSon(g.getId(),list);
        }
        return list;
    }

//    获取自己的所有父类
    public List<GoodsType> getAllba(int id,List<GoodsType> list){
        GoodsType typeBy = goodsTypeService.getTypeFather(id);
        if(typeBy!=null){
            list.add(typeBy);
            getAllba(typeBy.getId(),list);
        }
        return list;
    }
//    获取所有父类及其兄弟
    public List<GoodsType> getFB(int id,List<GoodsType> list){
//        先获取自己的所有兄弟
        List<GoodsType> typeborther = goodsTypeService.getTypeborther(id);
        System.out.println(typeborther);
//        如果兄弟存在，先把兄弟们放进去
        if(typeborther!=null&&typeborther.size()>0){
            list.addAll(typeborther);
        }
//        获取自己的父亲
            GoodsType typeFather = goodsTypeService.getTypeFather(id);
//            如果父亲存在
            if(typeFather!=null){
//                先把父亲添加进去，因为父亲的兄弟里没有父亲
                list.add(typeFather);
                if(typeFather.getFromid()!=0){
//                继续去获取父类的兄弟和父类父类的兄弟
                   getFB(typeFather.getId(),list);
                }
            }
        return list;
    }





}
